home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / SAT 2.3.7 / Demos / Demo ƒ / HeartQuest demo ƒ / scores.p < prev    next >
Encoding:
Text File  |  1995-08-11  |  12.9 KB  |  478 lines  |  [TEXT/PJMM]

  1. {================================================}
  2. {============= Score handling and display ==============}
  3. {================================================}
  4.  
  5. { Example file for Ingemars Sprite Animation Toolkit. }
  6. { © Ingemar Ragnemalm 1992 }
  7. { See doc files for legal terms for using this code. }
  8.  
  9. { This file manages the display and update of the game scores for HeartQuest.}
  10. { It holds routines for updating high score list, including asking for the name of}
  11. { the player, high score window etc. When making a new game, you will probably}
  12. { need to rewrite this unit a lot. }
  13.  
  14. unit scores;
  15.  
  16. interface
  17.     uses
  18. {$IFC UNDEFINED THINK_PASCAL}
  19.         Types, Quickdraw, ToolUtils, Resources, 
  20. {$ELSEC}
  21.         InterfacesUI, 
  22. {$ENDC}
  23.         TransSkel, SAT, Preferences, GameGlobals, SoundConst, CenterStuff;
  24.  
  25.     var
  26.         score: longint;
  27.  
  28.     procedure DoHighMenu (item: integer);
  29.     procedure InitScores;                    { Loads the high score list and the high score window. }
  30.     procedure ZeroScore;                     { Call this on New Game! }
  31.     procedure AddScore (amount: longint);    { Call this when the player gets points, or with addscore(0) just to redisplay. }
  32.     procedure AddScoreS (amount: longint);    { Call this to redisplay when the animation isn't running. }
  33.     procedure UpdateHigh;                    { Call this on game over! }
  34.  
  35. implementation
  36.  
  37. { Highscore record }
  38.     type
  39.         hsRec = record
  40.                 HighScores: array[0..10] of longint;
  41.                 HighPlayer: array[0..10] of str15;
  42.             end;
  43.         hsPtr = ^hsRec;
  44.         hsHnd = ^hsPtr;
  45.  
  46.  
  47.     var
  48.         hs, hsm: hsHnd; { m is for macho mode }
  49.         hsh, hshm: Handle;
  50.  
  51. {Filter function for AskHigh, ok = 1 and cancel = 4}
  52.     function Filter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: integer): boolean;
  53.         var
  54.             theChar: Char;
  55.             kind: integer;
  56.             item: Handle;
  57.             box: Rect;
  58.     begin
  59.         if theEvent.what = keyDown then
  60.             begin
  61.                 theChar := Char(BitAnd(theEvent.message, charCodeMask));
  62. {if BitAnd(theEvent.modifiers, cmdkey) <> 0 then}
  63. {if theChar = '.' then}
  64.                 if ((BitAnd(theEvent.modifiers, cmdkey) <> 0) and (theChar = '.')) or (theChar = char(27)) then {cmd-. or ESC}
  65.                     begin
  66.                         itemHit := 4;
  67. {Highlight the cancel button}
  68.                         GetDialogItem(theDialog, 4, kind, item, box);
  69.                         HiliteControl(ControlHandle(item), 1);
  70.  
  71.                         Filter := true;
  72.                         exit(Filter);
  73.                     end;
  74.                 if (theChar = char(13)) or (theChar = char(3)) then
  75.                     begin
  76.                         itemHit := 1;
  77. {Highlight the OK button}
  78.                         GetDialogItem(theDialog, 1, kind, item, box);
  79.                         HiliteControl(ControlHandle(item), 1);
  80.  
  81.                         Filter := true;
  82.                         exit(Filter);
  83.                     end;
  84.             end;
  85.         Filter := false;
  86.     end;
  87.  
  88. {Put a frame around a dialog item. There are better ways to do this, though. The right way}
  89. {is to draw the frame as response to an update event, not just when opening the dialog.}
  90.     procedure FrameDItem (dLog: DialogPtr; iNum: integer);
  91.         var
  92.             iBox: Rect;
  93.             iType: integer;
  94.             iHandle: Handle;
  95.             oldPenState: PenState;
  96.             tmpp: GrafPtr;
  97.     begin
  98.         GetPort(tmpp);
  99.         SetPort(dLog);
  100.         GetPenState(oldPenState);
  101.         GetDialogItem(dLog, iNum, iType, iHandle, iBox);
  102.         InsetRect(iBox, -4, -4);
  103.         PenSize(3, 3);
  104.         FrameRoundRect(iBox, 16, 16);
  105.         SetPenState(oldPenState);
  106.         SetPort(tmpp);
  107.     end;
  108.  
  109. { Ask for players name (at highscore) }
  110.     function AskHigh: str255;
  111.         var
  112.             dialog: DialogPtr;
  113.             oldPort: GrafPtr;
  114.             dRec: DialogRecord;
  115.             itemHit: integer;
  116.             itemHandle: Handle;
  117.             itemType, item: integer;
  118.             itemRect: Rect;
  119.             str: str255;
  120.             levelstr: str255;
  121. {$IFC GENERATINGPOWERPC }
  122.             filterProc: ProcPtr;
  123. {$ENDC}
  124.     begin
  125.         CenterDialog(highDlog);
  126.         GetPort(oldPort);
  127.         dialog := GetNewDialog(highDlog, @dRec, WindowPtr(-1));
  128.         ShowWindow(dialog);
  129.         SelectWindow(dialog);
  130.         SetPort(dialog);
  131.  
  132.         GetDialogItem(dialog, 3, itemType, itemHandle, itemRect);
  133.         SetDialogItemText(itemHandle, features^^.player);
  134.         SelectDialogItemText(dialog, 3, 0, 32767);
  135.         FrameDItem(dialog, 1);
  136.         itemHit := -1;
  137.  
  138. {$IFC GENERATINGPOWERPC }
  139.         filterProc := NewRoutineDescriptor(@Filter, uppModalFilterProcInfo, GetCurrentISA);
  140. {$ENDC}
  141.  
  142.         while (itemHit <> 1) and (itemHit <> 4) do { 1=ok, 4=cancel }
  143. {$IFC GENERATINGPOWERPC }
  144.             ModalDialog(filterProc, itemHit);
  145. {$ELSEC}
  146.         ModalDialog(@Filter, itemHit);
  147. {$ENDC}
  148.         if itemHit = 4 then
  149.             begin
  150.                 AskHigh := '';
  151.             end;
  152.         if itemHit = 1 then
  153.             begin
  154.                 GetDialogItem(dialog, 3, itemType, itemHandle, itemRect);
  155.                 GetDialogItemText(itemHandle, str);
  156.                 if length(str) > 15 then
  157.                     str := Copy(str, 1, 15);
  158.                 features^^.player := str;
  159.                 AskHigh := str;
  160.             end;
  161.         CloseDialog(dialog);
  162.         SetPort(oldPort);
  163.     end;
  164.  
  165. {     High Score window handlers }
  166.  
  167.     procedure HighUpdate (resized: boolean);
  168.         var
  169.             s: str255;
  170.             i: integer;
  171.     begin
  172.         EraseRect(theHigh^.portrect);
  173.         TextSize(9);
  174.  
  175.         moveto(10, 20);
  176.         DrawString(MyGetIndString(normalStrID)); {str 9: Normal high score list}
  177.         MoveTo(150, 20);
  178.         DrawString(MyGetIndString(machoStrID)); {str 10: Macho high score list}
  179.         MoveTo(0, 22);
  180.         LineTo(500, 22);
  181.         MoveTo(140, 0);
  182.         LineTo(140, 400);
  183.  
  184.         for i := 1 to 10 do
  185.             begin
  186.                 if not LastMacho and (i = LastHigh) then
  187.                     begin
  188.                         TextFace([bold]);
  189.                         ForeColor(redColor);
  190.                     end;
  191.                 moveto(10, i * 18 + 20);
  192.                 DrawString(hs^^.HighPlayer[i]);
  193.                 moveto(110, i * 18 + 20);
  194.                 NumToString(hs^^.HighScores[i], s);
  195.                 DrawString(s);
  196.  
  197.                 TextFace([]);
  198.                 ForeColor(BlackColor);
  199.                 if LastMacho and (i = LastHigh) then
  200.                     begin
  201.                         TextFace([bold]);
  202.                         ForeColor(redColor);
  203.                     end;
  204.                 moveto(150, i * 18 + 20);
  205.                 DrawString(hsm^^.HighPlayer[i]);
  206.                 moveto(250, i * 18 + 20);
  207.                 NumToString(hsm^^.HighScores[i], s);
  208.                 DrawString(s);
  209.  
  210.                 TextFace([]);
  211.                 ForeColor(BlackColor);
  212.             end;
  213.         TextSize(12);
  214.     end;
  215.  
  216.     procedure HighHalt;
  217.     begin
  218.         CloseWindow(theHigh);
  219.     end;
  220.  
  221.     function InternalAddScore (amount: longint): Rect;
  222.         var
  223.             s: str255;
  224.             r: Rect;
  225.     begin
  226.         score := score + amount;
  227.  
  228.         SetPort(gSAT.backScreen.port);
  229.         SetRect(r, gSAT.offSizeH - 49, 14, gSAT.offSizeH - 2, 155);
  230.         EraseRoundRect(r, 10, 10);
  231.         FrameRoundRect(r, 10, 10);
  232.         NumToString(Score, s);
  233.         MoveTo(gSAT.offSizeH - 47, 30);
  234.         DrawString(MyGetIndString(scoreStrID)); {str 11: Score: }
  235.         MoveTo(gSAT.offSizeH - 47, 50);
  236.         DrawString(s);
  237.  
  238.         if not bonusLevelRunning then
  239.             begin
  240.                 NumToString(bonus, s);
  241.                 MoveTo(gSAT.offSizeH - 47, 80);
  242.                 DrawString(MyGetIndString(bonusStrID)); {str 12: Bonus: }
  243.                 MoveTo(gSAT.offSizeH - 47, 100);
  244.                 DrawString(s);
  245.             end;
  246.  
  247.         NumToString(level, s);
  248.         MoveTo(gSAT.offSizeH - 47, 130);
  249.         DrawString(MyGetIndString(levelStrID)); {str 13: Level: }
  250.         MoveTo(gSAT.offSizeH - 47, 150);
  251.         DrawString(s);
  252.         InternalAddScore := r;
  253.     end;
  254.  
  255.     procedure AddScore (amount: longint);
  256.         var
  257.             s: str255;
  258.             r: Rect;
  259.             tmpport: grafptr;
  260.     begin
  261.         GetPort(tmpPort);
  262.         r := InternalAddScore(amount);
  263.         SATBackChanged(r); {Let SAT show it on screen}
  264.         SetPort(tmpPort);
  265.     end;
  266.  
  267.     procedure AddScoreS (amount: longint);
  268.         var
  269.             s: str255;
  270.             r: Rect;
  271.             tmpport: grafptr;
  272.     begin
  273.         GetPort(tmpPort);
  274.         r := InternalAddScore(amount);
  275.         CopyBits(gSAT.backScreen.port^.portbits, gSAT.wind.port^.portBits, r, r, srcCopy, nil);
  276.         CopyBits(gSAT.backScreen.port^.portbits, gSAT.offScreen.port^.portBits, r, r, srcCopy, nil);
  277.         SetPort(tmpPort);
  278.     end;
  279.  
  280.     procedure DoHighMenu (item: integer);
  281.         var
  282.             p: procptr;
  283.             i: integer;
  284.     begin
  285.         case item of
  286.             showhs: 
  287.                 begin
  288.                     ShowWindow(theHigh);
  289.                     SelectWindow(theHigh);
  290.                 end;
  291.             clearhs: 
  292.                 begin
  293.                     if SATQuestionStr(MyGetIndString(sureStrID)) then {str 14: Are you sure you want to erase the high scores?}
  294.                         begin
  295.                             for i := 1 to 10 do
  296.                                 begin
  297.                                     hs^^.HighScores[i] := 0;            { skall läsas från fil eller resurs }
  298.                                     hs^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15: Nobody}
  299.                                     hsm^^.HighScores[i] := 0;            { skall läsas från fil eller resurs }
  300.                                     hsm^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15}
  301.                                 end;
  302.                             hs^^.HighScores[0] := 10000;            { Lowscore }
  303.                             hsm^^.HighScores[0] := 10000;            { Lowscore }
  304.                             ChangedResource(handle(hs));
  305.                             ChangedResource(handle(hsm));
  306.                             HideWindow(theHigh);
  307.                         end;
  308.                 end;
  309.             otherwise
  310.                 ;
  311.         end;
  312.     end;
  313.  
  314.     procedure WindKey (theChar: char; theMods: integer);
  315.     begin
  316.     end;
  317.  
  318. { Call this on game over! }
  319.     procedure UpdateHigh;
  320.         var
  321.             num, len: integer;
  322.             name, s: str255;
  323.     begin
  324.         lastMacho := features^^.macho;
  325.  
  326.         if features^^.macho then
  327.             begin
  328.                 if score > hsm^^.HighScores[10] then
  329.                     begin
  330.                         num := 10;
  331.                         name := AskHigh;
  332.                         NumToString(level, s); {used below, to append level number}
  333. {Max 15 characters! We take some extra trouble to append '…' too.}
  334.                         len := length(stringof(' (', s, ')'));
  335.                         if length(name) > 15 - len then
  336.                             name := Concat(Copy(name, 1, 15 - len - 1), '…');
  337.  
  338.                         if name = '' then { alt length(name) = 0 }
  339.                             exit(updatehigh);
  340.                         while (hsm^^.HighScores[num - 1] < score) and (num > 1) do
  341.                             begin
  342.                                 hsm^^.HighScores[num] := hsm^^.HighScores[num - 1];
  343.                                 hsm^^.HighPlayer[num] := hsm^^.HighPlayer[num - 1];
  344.                                 num := num - 1;
  345.                             end;
  346.                         LastHigh := num; {Remember last high for the highscore display}
  347.                         hsm^^.HighScores[num] := score;
  348.                         hsm^^.HighPlayer[num] := stringof(name, ' (', s, ')'); {AskHigh;}
  349.                         ChangedResource(handle(hsm));
  350.                         HideWindow(theHigh);
  351.                         ShowWindow(theHigh);
  352.                         SelectWindow(theHigh);
  353.                     end;
  354.             end{ if macho }
  355.         else if score > hs^^.HighScores[10] then
  356.             begin
  357.                 num := 10;
  358.                 name := AskHigh;
  359.                 if length(name) > 15 then
  360.                     name := Concat(Copy(name, 1, 14), '…');
  361.  
  362.                 if name = '' then { alt length(name) = 0 }
  363.                     exit(updatehigh);
  364.                 while (hs^^.HighScores[num - 1] < score) and (num > 1) do
  365.                     begin
  366.                         hs^^.HighScores[num] := hs^^.HighScores[num - 1];
  367.                         hs^^.HighPlayer[num] := hs^^.HighPlayer[num - 1];
  368.                         num := num - 1;
  369.                     end;
  370.                 LastHigh := num; {Remember last high for the highscore display}
  371.                 hs^^.HighScores[num] := score;
  372.                 hs^^.HighPlayer[num] := name;
  373.                 ChangedResource(handle(hs));
  374.                 HideWindow(theHigh);
  375.                 ShowWindow(theHigh);
  376.                 SelectWindow(theHigh);
  377.             end;
  378.     end;
  379.  
  380.     procedure ZeroScore;
  381.     begin
  382.         score := 0;
  383.         LastHigh := -1;
  384.     end;
  385.  
  386. {This procedure copies a resource from the file applFile to prefFile (global file numbers,}
  387. {from the unit Preferences).}
  388. {OBSOLETE - should be replaced by the better code in Preferences.p!}
  389.     procedure OldCopyResource (resType: OSType; id: integer);
  390.         var
  391.             h, h2: Handle;
  392.             saveFile: integer;
  393.     begin
  394.         saveFile := CurResFile; {Look where we are so we can restore}
  395.         UseResFile(gAppFile);
  396.  
  397.         h := GetResource(resType, id); {Get res from the appl}
  398.         if h <> nil then
  399.             begin
  400.                 UseResFile(gPrefFile);
  401.                 h2 := GetResource(resType, id);
  402.                 if h2 = nil then {It doesn't already exist}
  403.                     begin
  404.                         DetachResource(h); {Detach it so we can move it.}
  405.                         AddResource(h, resType, id, ''); {Put it into the gPrefFile}
  406.                         ReleaseResource(h);
  407.                     end
  408.                 else {The res always exists. Don't copy.}
  409.                     begin
  410.                         ReleaseResource(h);
  411.                         ReleaseResource(h2);
  412.                     end;
  413.             end;
  414.         UseResFile(saveFile); {restore}
  415.     end;
  416.  
  417.     procedure InitScores;
  418.         var
  419.             i: integer;
  420.             ignoreErr: OSErr;
  421.     begin
  422.         if SetPrefFile(kPrefsFileName, kPrefCreator, kPrefType, gAppFile, gPrefFile, false) then {If a pref file was created, copy high scores to it!}
  423.             begin
  424.                 ignoreErr := CopyResource(gAppFile, gPrefFile, 'Bäst', 0); {Normal mode high scores}
  425.                 ignoreErr := CopyResource(gAppFile, gPrefFile, 'Bäst', 1); {Macho mode high scores}
  426.                 ignoreErr := CopyResource(gAppFile, gPrefFile, 'Feat', 0); {Settings}
  427.             end
  428.         else
  429.             gPrefFile := gAppFile; {If we have no pref file, let's make sure we UseResFile to something that exists.}
  430.  
  431.         lastHigh := -1; {no "last"}
  432.  
  433.         theHigh := GetNewWindow(theHighRes, nil, WindowPtr(-1));
  434.         SetPort(theHigh);
  435.         dummy := SkelWindow(theHigh, nil, @WindKey, @HighUpdate, nil, nil, @HighHalt, nil, false);
  436.  
  437.         UseResFile(gPrefFile); {set the resfile to the pref file, if any. If none, gPrefFile will be the app itself!}
  438.  
  439.         hs := hsHnd(GetResource('Bäst', 0));
  440.         if hs = nil then {Didn't exist - create it!}
  441.             begin
  442.                 hs := hsHnd(NewHandle(Sizeof(hsRec)));
  443.                 CheckNoMem(Ptr(hs));
  444.                 for i := 1 to 10 do
  445.                     begin
  446.                         hs^^.HighScores[i] := 0;
  447.                         hs^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15}
  448.                     end;
  449.                 hs^^.HighScores[0] := 10000;            { Lowscore }
  450.                 AddResource(handle(hs), 'Bäst', 0, 'High scores');
  451.             end
  452.         else {Did exist - check the size!}
  453.             if GetHandleSize(Handle(hs)) < sizeof(hsHnd) then
  454.                 SetHandleSize(Handle(hs), sizeof(hsHnd));
  455.  
  456.         hsm := hsHnd(GetResource('Bäst', 1));
  457.         if hsm = nil then {Didn't exist - create it!}
  458.             begin
  459.                 hsm := hsHnd(NewHandle(Sizeof(hsRec)));
  460.                 CheckNoMem(Ptr(hsm));
  461.                 for i := 1 to 10 do
  462.                     begin
  463.                         hsm^^.HighScores[i] := 0;            { skall läsas från fil eller resurs }
  464.                         hsm^^.HighPlayer[i] := MyGetIndString(nobodyStrID); {str 15}
  465.                     end;
  466.                 hsm^^.HighScores[0] := 10000;            { Lowscore }
  467.                 AddResource(handle(hsm), 'Bäst', 1, 'High scores');
  468.             end
  469.         else {Did exist - check the size!}
  470.             if GetHandleSize(Handle(hsm)) < sizeof(hsHnd) then
  471.                 SetHandleSize(Handle(hsm), sizeof(hsHnd));
  472.  
  473.         UseResFile(gAppFile);
  474.  
  475.         score := 0;
  476.     end;
  477.  
  478. end.